package com.css.fxfzysx.modules.ysxManagement.repository.impl;

import com.css.fxfzysx.modules.ysxManagement.entity.HaManageEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.PdLevelEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.PvRangeEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.PdLevelRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class PdLevelRepositoryImpl implements PdLevelRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void savePdLevel(PdLevelEntity it) {
        jdbcTemplate.update("insert into czt_pipeline_damage_level(cpdl_id,damage_level,damage_value,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?)", it.getCpdlId(), it.getDamageLevel(), it.getDamageValue(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updatePdLevel(PdLevelEntity it) {
        String sql = "update czt_pipeline_damage_level set damage_level= :damageLevel,damage_value= :damageValue,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cpdl_id= '"+it.getCpdlId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<PdLevelEntity> getAllPdLevel() {
        String sql = "select * from czt_pipeline_damage_level where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PdLevelEntity.class));
    }

    @Override
    public void deletePdLevelById(String pdLevelIds) {
        String sql = "update czt_pipeline_damage_level set del_flag = '1' where cpdl_id=?";
        jdbcTemplate.update(sql, pdLevelIds);
    }

    @Override
    public void savePvRange(PvRangeEntity it) {
        jdbcTemplate.update("insert into czt_pipe_vulnerability_range(cpvr_id,damage_state,range,description,create_user,create_time,del_flag,project_id,default_data) " +
                "values(?,?,?,?,?,?,?,?,?)", it.getCpvrId(), it.getDamageState(), it.getRange(), it.getDescription(),it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getProjectId(),it.getDefaultData());
    }

    @Override
    public void updatePvRange(PvRangeEntity it) {
        String sql = "update czt_pipe_vulnerability_range set damage_state= :damageState,range= :range,description= :description,update_user= :updateUser,update_time= :updateTime,project_id= :projectId" +
                " where cpvr_id= '"+it.getCpvrId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<PvRangeEntity> getAllPvRange() {
        String sql = "select * from czt_pipe_vulnerability_range where del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PvRangeEntity.class));
    }

    @Override
    public void deletePvRangeById(String pvRangeId) {
        String sql = "update czt_pipe_vulnerability_range set del_flag = '1' where cpvr_id=?";
        jdbcTemplate.update(sql, pvRangeId);
    }

    @Override
    public void deletePdLevelByProjectId(String arrangeId) {
        String sql = "update czt_pipeline_damage_level set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }

    @Override
    public void deletePvRangeByProjectId(String arrangeId) {
        String sql = "update czt_pipe_vulnerability_range set del_flag = '1' where project_id=?";
        jdbcTemplate.update(sql, arrangeId);
    }
}
